package com.hackerrank.challenges.subsequenceweighting;

import java.util.Scanner;

public class Solution {
	private static long maxWeight(int[] a, int[] w) {
		long mw[] = new long[a.length + 1];
		for(int li = a.length - 2; li >= -1; li--) {
			int l = li < 0 ? 0 : a[li], u = Integer.MAX_VALUE;
			for(int i = li + 1; i < a.length; i++) {
				if(a[i] > l && a[i] <= u) {
					if(mw[i + 1] > mw[li + 1])
						mw[li + 1] = mw[i + 1];
					u = a[i];
				}
			}
			if(li >= 0)
				mw[li + 1] += w[li];
		}
		return mw[0];
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int C = in.nextInt();
		for(int t = 0; t < C; t++) {
			int N = in.nextInt(), a[] = new int[N], w[] = new int[N];
			for(int i = 0; i < N; i++)
				a[i] = in.nextInt();
			for(int i = 0; i < N; i++)
				w[i] = in.nextInt();
			System.out.println(maxWeight(a, w));
		}
	}
}
